package com.mongo.proxy;

import com.mongo.service.MongoMapper;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * mongo代理实现
 * @author xiazhun
 * @create 2022/9/15
 */
public class MongoServiceProxy<T> implements InvocationHandler {
    private final Class<T> interfaces;

    private final MongoMapper<T> mapper;


    public MongoServiceProxy(Class<T> interfaces, MongoMapper<T> mapper) {
        this.interfaces = interfaces;
        this.mapper = mapper;
    }

    @Override
    @SuppressWarnings("all")
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (method.getDeclaringClass().equals(interfaces)) {
            return method.getName();
        } else {
            if (mapper == null){
                throw new IllegalArgumentException("mapper can be not null");
            }
            return method.invoke(mapper,args);
        }
    }
}
